﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DiscoveryLogic.Analytics.DataObjects;

namespace DiscoveryLogic.Analytics.Modules.Entities
{
	/// <summary>
	/// Interaction logic for PersonListView.xaml
	/// </summary>
	public partial class PersonListView : UserControl
	{
		public delegate void OnPersonSelected(PersonDO person);

		public event OnPersonSelected PersonSelected;

		private PersonListPresenter _Presenter;

		public PersonListView()
		{
			InitializeComponent();
		}

		public PersonListView(PersonListPresenter presneter)
			: this()
		{
			_Presenter = presneter;
			_Presenter.View = this;
		}

		public List<PersonDO> Persons
		{
			get { return this.DataContext as List<PersonDO>; }
			set
			{
			    this.DataContext = value;
                if (value == null || value.Count == 0)
                {
                    this.Visibility = System.Windows.Visibility.Collapsed;
                }
                else
                {
                    this.Visibility = System.Windows.Visibility.Visible;
                }
			}
		}

        public void SubscribeEventHandler(PersonDO person)
        {
            this._Presenter.RegisterPersonSelectionChangedEventHandler(person);
        }

	    private void lvPersons_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			if(e.AddedItems.Count==1)
			{
				PersonDO person = e.AddedItems[0] as PersonDO;
				if (this.PersonSelected != null)
					this.PersonSelected(person);
			}
		}

        public void SelectPerson(PersonDO person)
        {
            if(this.Persons !=null && person !=null)
            {
                for(int i=0;i<this.Persons.Count;i++)
                {
                    if(this.Persons[i].PersonID==person.PersonID)
                    {
                        this.lvPersons.SelectedIndex = i;
                        break;
                    }
                }
            }
        }
	}
}
